﻿using BLL;
using Model;
using System;
using System.Data;
using System.Web.UI.WebControls;

namespace Thksoft.UserCotrols
{
    public partial class Basic_TreeView : System.Web.UI.UserControl
    {
        private Sys_CompanyBLL scb = new Sys_CompanyBLL();
        private Sys_DepartmentBLL sdb = new Sys_DepartmentBLL();

        /// <summary>
        /// 绑定TreeView(递归算法)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string COMPANYID = ((Sys_User)System.Web.HttpContext.Current.Session["SysUserInfo"]).USER_COMPANY.ToString();
                if (COMPANYID != null)
                {
                    Sys_Company model = scb.GetInfoById(COMPANYID);
                    FileTree.ImageSet = TreeViewImageSet.Custom;

                    BindFatherNodes(model.CMY_ID.ToString());

                    TreeSet();

                    FileTree.ExpandAll();
                }
            }
        }

        /// <summary>
        /// 根节点
        /// </summary>
        /// <param name="CurAgentID"></param>
        public void BindFatherNodes(string CurAgentID)
        {
            //清空节点
            FileTree.Nodes.Clear();

            DataTable Fatherdt = scb.GetInfoByPId(Convert.ToInt32(CurAgentID));

            foreach (DataRow item in Fatherdt.Rows)
            {
                string NodesId = item["CMY_ID"].ToString();

                string NodesName = item["CMY_NAME"].ToString();

                TreeNode FatherTreeNode = new TreeNode(); //创建一个节点对象

                FatherTreeNode.Text = NodesName;//添加显示文本

                FatherTreeNode.Value = NodesId.ToString(); //添加节点的值

                FatherTreeNode.NavigateUrl = "javascript:ToReport(" + NodesId + ",0,'" + NodesName + "')";

                BindsonNodes(NodesId, FatherTreeNode);//调用添加子节点的方法

                BindChildNodes(NodesId, FatherTreeNode);//接续调用添加子节点方法
                if (item["CMY_ID"].ToString() == CurAgentID)
                {
                    FileTree.Nodes.Add(FatherTreeNode);//添加节点到树形控件上
                }
            }
        }

        /// <summary>
        /// 添加子节点的方法
        /// </summary>
        /// <param name="NodeId">节点编号</param>
        /// <param name="TwoTreeNode">节点对象</param>
        public void BindsonNodes(string NodeId, TreeNode TwoTreeNode)
        {
            DataTable dtDept = scb.GetInfoByParentId(NodeId);
            foreach (DataRow item in dtDept.Rows)
            {
                if (item["CMY_ID"].ToString() != NodeId)
                {
                    string NodesId = item["CMY_ID"].ToString();
                    string NodesName = item["CMY_NAME"].ToString();

                    TreeNode ChildTreeNode = new TreeNode(); //创建一个节点对象

                    ChildTreeNode.Text = NodesName;//添加显示文本

                    ChildTreeNode.Value = NodesId.ToString(); //添加节点的值

                    ChildTreeNode.NavigateUrl = "javascript:ToReport(" + NodesId + ",0,'" + NodesName + "')";

                    BindsonNodes(NodesId, ChildTreeNode);//接续调用添加子节点方法

                    BindChildNodes(NodesId, ChildTreeNode);//接续调用添加子节点方法

                    TwoTreeNode.ChildNodes.Add(ChildTreeNode);//添加节点到根节点上
                }
            }
        }

        /// <summary>
        /// 添加子节点的方法
        /// </summary>
        /// <param name="NodeId">节点编号</param>
        /// <param name="TwoTreeNode">节点对象</param>
        public void BindChildNodes(string NodeId, TreeNode TwoTreeNode)
        {
            DataTable dtDept = sdb.GetInfoByCmyID(NodeId, true);
            foreach (DataRow item in dtDept.Rows)
            {
                string NodesId = item["DEPTID"].ToString();
                string NodesName = item["DEPTNAME"].ToString();

                TreeNode ChildTreeNode = new TreeNode(); //创建一个节点对象

                ChildTreeNode.Text = NodesName;//添加显示文本

                ChildTreeNode.Value = NodesId.ToString(); //添加节点的值

                ChildTreeNode.NavigateUrl = "javascript:ToReport(" + NodesId + ",1,'" + NodesName + "')";

                BindNextNodes(NodesId, ChildTreeNode);//接续调用添加子节点方法

                TwoTreeNode.ChildNodes.Add(ChildTreeNode);//添加节点到根节点上
            }
        }

        /// <summary>
        /// SQLSERVER添加子节点的方法
        /// </summary>
        /// <param name="NodeId">节点编号</param>
        /// <param name="TwoTreeNode">节点对象</param>
        public void BindNextNodes(string NodeId, TreeNode TwoTreeNode)
        {
            DataTable dtDept = sdb.GetInfoByParentDeptID(NodeId);
            foreach (DataRow item in dtDept.Rows)
            {
                if (item["DEPTID"].ToString() != NodeId)
                {
                    string NodesId = item["DEPTID"].ToString();
                    string NodesName = item["DEPTNAME"].ToString();

                    TreeNode ChildTreeNode = new TreeNode(); //创建一个节点对象

                    ChildTreeNode.Text = NodesName;//添加显示文本

                    ChildTreeNode.Value = NodesId.ToString(); //添加节点的值

                    ChildTreeNode.NavigateUrl = "javascript:ToReport(" + NodesId + ",1,'" + NodesName + "')";

                    BindNextNodes(NodesId, ChildTreeNode);//接续调用添加子节点方法

                    TwoTreeNode.ChildNodes.Add(ChildTreeNode);//添加节点到根节点上
                }
            }
        }

        public void TreeSet()
        {
            for (int i = 0; i < FileTree.Nodes.Count; i++)
            {
                if (FileTree.Nodes[i].ChildNodes.Count > 0)
                {
                    FileTree.Nodes[i].ImageUrl = "~/Images/levelOnesmall.jpg";

                    for (int j = 0; j < FileTree.Nodes[i].ChildNodes.Count; j++)
                    {
                        if (FileTree.Nodes[i].ChildNodes[j].ChildNodes.Count > 0)
                        {
                            FileTree.Nodes[i].ChildNodes[j].ImageUrl = "~/Images/levelTwo.jpg";

                            for (int k = 0; k < FileTree.Nodes[i].ChildNodes[j].ChildNodes.Count; k++)
                            {
                                if (FileTree.Nodes[i].ChildNodes[j].ChildNodes[k].ChildNodes.Count > 0)
                                {
                                    FileTree.Nodes[i].ChildNodes[j].ChildNodes[k].ImageUrl = "~/Images/levelThr_01.jpg";

                                    for (int m = 0; m < FileTree.Nodes[i].ChildNodes[j].ChildNodes[k].ChildNodes.Count; m++)
                                    {
                                        FileTree.Nodes[i].ChildNodes[j].ChildNodes[k].ChildNodes[m].ImageUrl = "~/Images/levelFour_01.jpg";
                                    }
                                }
                                else
                                {
                                    FileTree.Nodes[i].ChildNodes[j].ChildNodes[k].ImageUrl = "~/Images/levelFour_01.jpg";
                                }
                            }
                        }
                        else
                        {
                            FileTree.Nodes[i].ChildNodes[j].ImageUrl = "~/Images/levelFour_01.jpg";
                        }
                    }
                }
                else
                {
                    FileTree.Nodes[i].ImageUrl = "~/Images/levelFour_01.jpg";
                }
            }
        }
    }
}